package com.xuecheng.media.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.xuecheng.api.media.model.MediaApi;
import com.xuecheng.api.media.model.aliyun.VodUploadRequest;
import com.xuecheng.api.media.model.aliyun.VodUploadToken;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.common.constant.MediaErrorCode;
import com.xuecheng.media.common.utils.AliyunVODUtil;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class MediaController implements MediaApi {

    @Autowired
    private MediaService  mediaService;

    @Value("${aliyun.region}")
    private String region;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    @PostMapping("media/vod-token")  //获得文件上传凭证
    public VodUploadToken generateVodToken(@RequestBody VodUploadRequest request) {
        String title = request.getTitle();
        String fileName = request.getFileName();
        VodUploadToken vodUploadToken = null;
        try {
            //初始化客户端
            DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);

            //获取上传凭证与地址
            CreateUploadVideoResponse uploadVideo = AliyunVODUtil.createUploadVideo(client, title, fileName);

            //将上传凭证与地址封装到实体类 中返回数据
            vodUploadToken = new VodUploadToken();

            vodUploadToken.setRequestId(uploadVideo.getRequestId());
            vodUploadToken.setVideoId(uploadVideo.getVideoId());
            vodUploadToken.setUploadAddress(uploadVideo.getUploadAddress());
            vodUploadToken.setUploadAuth(uploadVideo.getUploadAuth());
        } catch (Exception e) {
            // e.printStackTrace();
            ExceptionCast.cast(MediaErrorCode.E_140011);
        }

        return vodUploadToken;
    }

    @GetMapping("media/refresh-vod-token/{videoId}") //刷新文上传凭证
    public VodUploadToken refreshVodToken(@PathVariable String videoId) {
        VodUploadToken vodUploadToken = null;
        try {
            //初始化客户端
            DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
            //刷新上传凭证
            RefreshUploadVideoResponse VideoResponse = AliyunVODUtil.refreshUploadVideo(client, videoId);

            //将上传凭证与地址封装到实体类 中返回数据
            vodUploadToken = new VodUploadToken();

            vodUploadToken.setRequestId(VideoResponse.getRequestId());
            vodUploadToken.setVideoId(videoId);
            vodUploadToken.setUploadAddress(VideoResponse.getUploadAddress());
            vodUploadToken.setUploadAuth(VideoResponse.getUploadAuth());
        } catch (Exception e) {
           // e.printStackTrace();
            ExceptionCast.cast(MediaErrorCode.E_140015);
        }

        return vodUploadToken;
    }

    @PostMapping("media")
    public MediaDTO saveMedia(@RequestBody MediaVO mediaVO) {

        Long companyId = SecurityUtil.getCompanyId();

        MediaDTO mediaDTO = new MediaDTO();
        mediaDTO.setCompanyId(companyId);
        mediaDTO.setFileId(mediaVO.getFileId());
        mediaDTO.setFilename(mediaVO.getFilename());
        mediaDTO.setTags(mediaVO.getTags());
        mediaDTO.setType(mediaVO.getType());
        MediaDTO resultDto = mediaService.createMedia(mediaDTO);
        return resultDto;
    }

    @PostMapping("media/list")
    public PageVO<MediaDTO> queryMedia(PageRequestParams params, @RequestBody MediaVO mediaVO) {
        Long companyId = SecurityUtil.getCompanyId();

        MediaDTO mediaDTO = new MediaDTO();
        mediaDTO.setCompanyId(companyId);
        if (!ObjectUtils.isEmpty(mediaVO.getFilename())){
            mediaDTO.setFilename(mediaVO.getFilename());
        }
        if (!ObjectUtils.isEmpty(mediaVO.getType())){
            mediaDTO.setType(mediaVO.getType());
        }

        PageVO<MediaDTO> mediaDTOS = mediaService.queryMedia(params, mediaDTO);

        return mediaDTOS;
    }

    @GetMapping("media/preview/{mediaId}")
    public String queryMediaUrl(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();
        String mediaURL = mediaService.getMediaURL(mediaId, companyId);
        return mediaURL;
    }

    @DeleteMapping("media/{mediaId}")
    public void removeMedia(@PathVariable Long mediaId) {
        Long companyId = SecurityUtil.getCompanyId();
        mediaService.removeMedia(mediaId,companyId);
    }

    @PostMapping("m/media/list")
    public PageVO<MediaDTO> queryMedias(PageRequestParams params, @RequestBody MediaVO mediaVO) {
        MediaDTO mediaDTO = new MediaDTO();

        mediaDTO.setFilename(mediaVO.getFilename());
        mediaDTO.setType(mediaVO.getType());

        return mediaService.queryMediaM(params,mediaDTO);
    }

    @PostMapping("m/media/audit")
    public void createMediaM(MediaVO mediaVO) {
        MediaDTO mediaDTO = new MediaDTO();
        mediaDTO.setId(mediaVO.getId().longValue());
        mediaDTO.setAuditStatus(mediaVO.getAuditStatus());
        mediaDTO.setAuditMind(mediaVO.getAuditMind());

        mediaService.createMediaM(mediaDTO);
    }

    @GetMapping("l/media/{mediaId}")
    public RestResponse<MediaDTO> queryMediaById(@PathVariable Long mediaId) {
        RestResponse<MediaDTO> dtoResponse = mediaService.getByIdMedia(mediaId);
        return dtoResponse;
    }
}
